package com.niodata.dp.core.resource;

import java.util.HashSet;
import java.util.Set;

public class CombinedResourceDetailResolver implements ResourceDetailResolver {

  private final Set<ResourceDetailResolver> resolverSet = new HashSet<ResourceDetailResolver>();

  public void registerResolver(ResourceDetailResolver resolver) {
    resolverSet.add(resolver);
  }

  @Override
  public ResourceDetail resolve(Resource resource) {
    for (ResourceDetailResolver resolver : resolverSet) {
      if (resolver.supportResourceType(resource.resourceType().getClass())
            && !resolver.getClass().equals(this.getClass())) {
        return resolver.resolve(resource);
      }
    }
    return null;
  }

  @Override
  public boolean supportResourceType(Class<? extends ResourceType> clazz) {
    return ResourceType.class.isAssignableFrom(clazz);
  }

  public Set<ResourceDetailResolver> getResolverSet() {
    return this.resolverSet;
  }

}
